import Cookies from 'js-cookie'
import {recordData} from "@/apis/record";
export function buttonClick(pagePath, buttonName) {
    // 创建一个新的点击记录对象
    // const clickRecord = {
    //     pagePath,
    //     buttonName,
    //     buttonId,
    //     clickCount,
    // };
    // 从 cookie 中获取现有数据
    const clickRecordsStr = Cookies.get('clickRecord');
    let clickRecord = clickRecordsStr ? JSON.parse(clickRecordsStr) : {};
    // 确保 clickRecord 中有当前页面路径的记录
    if (!clickRecord[pagePath]) {
        clickRecord[pagePath] = {};
    }
    // 更新或添加新的点击记录
    if (!clickRecord[pagePath][buttonName]) {
        clickRecord[pagePath][buttonName] = {
            clickCount: 0,
        };
    }
    // 增加点击次数并记录点击时间
    clickRecord[pagePath][buttonName].clickCount++;

    // 将更新后的数据存储回 cookie
    Cookies.set('clickRecord', JSON.stringify(clickRecord));//, { expires: 365 }
    // click_pv: clickRecord[pagePath][buttonName].clickCount
    recordData({
        current_url: pagePath,
        fingerprint: Cookies.get('fingerprint'),
        click_button: buttonName,
    }).then((res) => {
        if (res.code === 200 && res.status === 1) {
            console.log('Data reported successfully');
        } else {
            console.error('Error reporting data:', res);
        }
    });
}